using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace SkypieeClient
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : System.Windows.Window
    {
        public Skypiee.Core.SkypieeCore SCore = new Skypiee.Core.SkypieeCore();

        public Window1()
        {
            InitializeComponent();
            //Avatar1.MouseDown += new MouseButtonEventHandler(Avatar1_MouseDown);
            //Avatar1.MouseMove += new MouseEventHandler(Avatar1_MouseMove);
            SCore.Start();
            //SCore.CheckSkype();
            SCore.LoadGroups();
            LoadData();
            TabControlMain.Items.RemoveAt(0);
        }

        private void LoadData()
        {
            AddNewTab(null, CheckBoxOnline.IsChecked.Value);
            foreach (SKYPE4COMLib.IGroup ig in SCore.Groups)
            {
                AddNewTab(ig, CheckBoxOnline.IsChecked.Value);
            }
        }

        private void CheckBoxOnline_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            CheckedChanged();
        }

        private void CheckedChanged()
        {
            foreach (TabItem ti in TabControlMain.Items)
            {
                SKYPE4COMLib.IGroup ig = (SKYPE4COMLib.IGroup)ti.Tag;
                if (ig != null)
                {
                    SCore.LoadContacts(ig, CheckBoxOnline.IsChecked.Value, true, !CheckBoxOnline.IsChecked.Value);

                    List<Skypiee.Core.SkypieeUser> users = new List<Skypiee.Core.SkypieeUser>();
                    users.AddRange(SCore.Contacts.ToArray());

                    Binding b1 = new Binding();
                    b1.Source = users;
                    ((TabContent)ti.Content).ListBox1.SetBinding(ListBox.ItemsSourceProperty, b1);
                }
                else
                {
                    SCore.LoadAllContacts();
                    List<Skypiee.Core.SkypieeUser> users = new List<Skypiee.Core.SkypieeUser>();
                    users.AddRange(SCore.Contacts.ToArray());

                    Binding b1 = new Binding();
                    b1.Source = users;
                    ((TabContent)ti.Content).ListBox1.SetBinding(ListBox.ItemsSourceProperty, b1);
                }
            }
        }

        private void CheckBoxOnline_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
            CheckedChanged();
        }

        private void AddNewTab(SKYPE4COMLib.IGroup ig, bool OnlineOnly)
        {
            TabItem ti = new TabItem();
            if (ig == null)
            {
                SCore.LoadAllContacts();
                ti.Header = "All Contacts";
            }
            else
            {
                SCore.LoadContacts(ig);
                if (ig.Type == SKYPE4COMLib.TGroupType.grpCustomGroup)
                    ti.Header = ig.DisplayName;
                else
                    ti.Header = SCore.GetGroupTypeName(ig);
                ti.Tag = ig;
            }

            ti.Style = ((TabItem)TabControlMain.Items[0]).Style;
            TabControlMain.Items.Add(ti);

            TabContent tc = new TabContent();
            tc.Margin = new Thickness(0);
            Binding b = new Binding();
            b.ElementName = "uiZoomSlider";
            b.Path = new PropertyPath("Value");
            tc.SetBinding(TabContent.ScaleProperty, b);
            ti.Content = tc;

            tc.ListBox1.DisplayMemberPath = "DisplayName";
            tc.ListBox1.SelectedValuePath = "Handle";

            List<Skypiee.Core.SkypieeUser> users = new List<Skypiee.Core.SkypieeUser>();
            users.AddRange(SCore.Contacts.ToArray());

            Binding b1 = new Binding();
            b1.Source = users;
            tc.ListBox1.SetBinding(ListBox.ItemsSourceProperty, b1);
        }


        private void MenuItemExit_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

    }
}